package com.acce.gxsc.page.supervision.outCarDetail;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.NetOutBatchCarBean;
import com.acce.gxsc.model.netBean.NetOutInCarBean;
import com.acce.gxsc.page.outCar.batchDetail.BatchDetailActivity;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;
import com.acce.sdk.widget.EditWithTitle;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */

public class OutCarDetailActivity extends BaseMvpActivity<OutCarDetailView, OutCarDetailPresenter> implements OutCarDetailView {

    String queryType   = "";
    String queryId     = "";
    String plateNum    = "";
    String containNo   = "";
    String hangNum     = "";
    String contactName = "";
    LayoutInflater                 inflater;
    ArrayList<NetOutInCarBean>     data;
    CommonAdapter<NetOutInCarBean> mCommonAdapter;

    @Bind(R.id.tv_header)
    TextView           mTvHeader;
    @Bind(R.id.rv_out_car_list)
    RecyclerView       mRvOutCarList;

    @Override
    protected OutCarDetailPresenter createPresenter() {
        return new OutCarDetailPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_out_car_detail_1);
        ButterKnife.bind(this);
        inflater = LayoutInflater.from(this);

        initView();
        initData();
    }

    private void initData() {
        if (getIntent().hasExtra("queryType")) {
            queryType = getIntent().getStringExtra("queryType");
        }
        if (getIntent().hasExtra("queryId")) {
            queryId = getIntent().getStringExtra("queryId");
        }
        if (getIntent().hasExtra("plateNum")) {
            plateNum = getIntent().getStringExtra("plateNum");
        }
        if (getIntent().hasExtra("containNo")) {
            containNo = getIntent().getStringExtra("containNo");
        }
        if (getIntent().hasExtra("hangNum")) {
            hangNum = getIntent().getStringExtra("hangNum");
        }
        if (getIntent().hasExtra("contactName")) {
            contactName = getIntent().getStringExtra("contactName");
        }
        mPresenter.loadInCarList(queryType, queryId, plateNum, containNo, hangNum, contactName);
    }

    private void initView() {

        data = new ArrayList<>();
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRvOutCarList.setLayoutManager(linearLayoutManager);
        mRvOutCarList.setAdapter(mCommonAdapter = new CommonAdapter<NetOutInCarBean>(this, R.layout.item_out_car_1, data) {
            @Override
            protected void convert(ViewHolder h, final NetOutInCarBean inCarBean, final int position) {


                h.setOnClickListener(R.id.ll_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent outCarDetailIntent = new Intent(OutCarDetailActivity.this, com.acce.gxsc.page.outCar.detail.OutCarDetailActivity.class);
                        outCarDetailIntent.putExtra("queryId", inCarBean.inCarId);
                        startActivity(outCarDetailIntent);
                    }
                });

                h.setText(R.id.tv_out_car, "核对正确");
                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_box_no), "挂号/柜号：", Tools.getString1(inCarBean.hangNum, inCarBean.containNo));
                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_car_no), "车牌号码：", inCarBean.plateNum);
//                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_incar_time), "进场时间：", inCarBean.createTime.substring(0, 19));
                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_stay_time), "驻留时间：", Tools.getStayTime(inCarBean.createTime));
                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_type), "登记类型：", inCarBean.diliveryType);
                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_incar_id), "进场单号：", inCarBean.inCarId);
                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_park_area), "停放区域：", inCarBean.parkArea);
                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_park_position), "车    位：", inCarBean.parkPosition);
                h.setText(R.id.tv_incar_position, Tools.getString(inCarBean.inBatchGateNo));
                h.setText(R.id.tv_incar_name, Tools.getString(inCarBean.inBatchRegistrant));
                h.setText(R.id.tv_incar_time, Tools.getTime(inCarBean.createTime));
                h.setText(R.id.tv_supervision_state, Tools.getSupervisionState(inCarBean.superviseOther));
                h.setText(R.id.tv_supervision_name, Tools.getString(inCarBean.superviseRegistrant));
                h.setText(R.id.tv_supervision_time, Tools.getTime(inCarBean.superviseRegisterDate, inCarBean.superviseRegisterTime));
                h.setText(R.id.tv_outcar_state, Tools.getOutCarState(inCarBean.outCarStatus));
                h.setText(R.id.tv_outcar_name, Tools.getString(inCarBean.outCarGateNo));
                h.setText(R.id.tv_outcar_time, Tools.getTime(inCarBean.outCarRegisterDate, inCarBean.outCarRegisterTime));

                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_car_type), "车辆类型：", inCarBean.carType);
                Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_company_name), "运输公司：", inCarBean.underCompany);
                h.setText(R.id.tv_registercar_position, Tools.getString(inCarBean.confirmGateName));
                h.setText(R.id.tv_registercar_name, Tools.getString(inCarBean.confirmRegistrant));
                h.setText(R.id.tv_registercar_time, Tools.getTime(inCarBean.confirmRegisterDate, inCarBean.confirmRegisterTime));
                if ("1".equals(inCarBean.outCarStatus)) {
                    Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_state), "已离场");
                    h.getView(R.id.tv_out_car).setEnabled(false);
                    h.getView(R.id.tv_out_car).setBackgroundResource(R.color.color_e4e4e4);
                    h.getView(R.id.tv_diff_out_car).setEnabled(false);
                    h.getView(R.id.tv_diff_out_car).setBackgroundResource(R.color.color_e4e4e4);
                } else {
                    Tools.setStringWithTitle(h.<TextView>getView(R.id.tv_state), "未离场");
                }
                h.getView(R.id.tv_out_car).setEnabled(!"1".equals(inCarBean.superviseRegisterDate));
                h.getView(R.id.tv_out_car).setBackgroundResource("1".equals(inCarBean.superviseRegisterDate) ? R.color.color_e4e4e4 : R.color.red_c);
                h.getView(R.id.tv_diff_out_car).setEnabled(!"1".equals(inCarBean.superviseRegisterDate));
                h.getView(R.id.tv_diff_out_car).setBackgroundResource("1".equals(inCarBean.superviseRegisterDate) ? R.color.color_e4e4e4 : R.color.red_5);
                h.setOnClickListener(R.id.tv_out_car, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (StringUtils.isEmpty(inCarBean.inCarId)) {
                            ToastUtils.show("未找到该进场单！");
                            return;
                        }
                        final AlertDialog.Builder builder1 = new AlertDialog.Builder(OutCarDetailActivity.this);
                        final AlertDialog alertDialog1 = builder1
                                .setTitle("提示")
                                .setMessage("已确认该车信息？")
                                .setNegativeButton("取消", null)
                                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        mPresenter.toOutCar(inCarBean.inCarId, "");
                                    }
                                }).create();
                        alertDialog1.setCanceledOnTouchOutside(false);
                        alertDialog1.show();
                    }
                });
                h.setOnClickListener(R.id.tv_diff_out_car, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (StringUtils.isEmpty(inCarBean.inCarId)) {
                            ToastUtils.show("未找到该进场单！");
                            return;
                        }
                        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_diff_supervision, (ViewGroup) findViewById(R.id.dialog));
                        final EditWithTitle ewtCarNo = ButterKnife.findById(layout, R.id.ewt_car_no);
                        final EditWithTitle ewtHangNo = ButterKnife.findById(layout, R.id.ewt_hang_no);
                        final EditWithTitle ewtBoxNo = ButterKnife.findById(layout, R.id.ewt_box_no);
                        final EditWithTitle ewtArea = ButterKnife.findById(layout, R.id.ewt_area);
                        final EditWithTitle ewtGoods = ButterKnife.findById(layout, R.id.ewt_goods);
                        final EditWithTitle ewtRemark = ButterKnife.findById(layout, R.id.ewt_remark);

                        final AlertDialog.Builder builder = new AlertDialog.Builder(OutCarDetailActivity.this);
                        final AlertDialog alertDialog = builder
                                .setView(layout)
                                .setTitle("请填写差异信息")
                                .setNegativeButton("取消", null)
                                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        String carNo = ewtCarNo.getContent();
                                        String hangNo = ewtHangNo.getContent();
                                        String boxNo = ewtBoxNo.getContent();
                                        String area = ewtArea.getContent();
                                        String goods = ewtGoods.getContent();
                                        String remark = ewtRemark.getContent();
                                        if (StringUtils.isEmpty(carNo) && StringUtils.isEmpty(hangNo) && StringUtils.isEmpty(boxNo) && StringUtils.isEmpty(area) && StringUtils.isEmpty(goods) && StringUtils.isEmpty(remark)) {
                                            ToastUtils.show("至少填写一项差异信息");
                                            return;
                                        }
                                        mPresenter.toOutCar(inCarBean.inCarId, Tools.getString4(carNo, hangNo, boxNo, area, goods, remark));
                                    }
                                }).create();
                        alertDialog.setCanceledOnTouchOutside(false);
                        alertDialog.show();
                    }
                });
                LinearLayout llContent = h.getView(R.id.ll_content);
                llContent.removeAllViews();
                for (final NetOutBatchCarBean batchBean : inCarBean.batchCarList) {
                    View chile = inflater.inflate(R.layout.item_out_car_1_batch, llContent, false);
                    ViewHolder holder = new ViewHolder(OutCarDetailActivity.this, chile);
                    holder.setText(R.id.tv_inbatch_id, batchBean.inBatchId);
                    holder.setText(R.id.tv_contact_name, batchBean.contactName);
                    holder.setText(R.id.tv_goods_info, batchBean.goodsInfo);
                    if (StringUtils.isEmpty(batchBean.fee2Id) || StringUtils.isEmpty(batchBean.fee3Id)) {
                        holder.setText(R.id.tv_fee_1, "1#门单据");
                        holder.setText(R.id.tv_fee_2, "1#门单据");
                        holder.setBackgroundColor(R.id.ll_state, Tools.getColor(R.color.yellow));
                        holder.setText(R.id.tv_state, "？");
                        holder.setTextColor(R.id.tv_fee_1, Color.WHITE);
                        holder.setTextColor(R.id.tv_fee_2, Color.WHITE);
                        holder.setTextColor(R.id.tv_state, Color.WHITE);
                        holder.setTextColor(R.id.tv_contact_name, Color.WHITE);
                        holder.setTextColor(R.id.tv_goods_info, Color.WHITE);
                    } else {
                        if ("1".equals(batchBean.fee2Id) || batchBean.fee2Id.startsWith("33") || "1".equals(batchBean.fee3Id) || batchBean.fee3Id.startsWith("33")) {
                            holder.setBackgroundColor(R.id.ll_state, Tools.getColor(R.color.morning_glory));
                            holder.setText(R.id.tv_state, "×");
                            holder.setTextColor(R.id.tv_fee_1, Color.RED);
                            holder.setTextColor(R.id.tv_fee_2, Color.RED);
                            holder.setTextColor(R.id.tv_state, Color.RED);
                            holder.setTextColor(R.id.tv_contact_name, Color.RED);
                            holder.setTextColor(R.id.tv_goods_info, Color.RED);
                        } else {
                            holder.setBackgroundColor(R.id.ll_state, Color.WHITE);
                            holder.setText(R.id.tv_state, "√");
                            holder.setTextColor(R.id.tv_fee_1, Tools.getColor(R.color.text_lgray));
                            holder.setTextColor(R.id.tv_fee_2, Tools.getColor(R.color.text_lgray));
                            holder.setTextColor(R.id.tv_state, Tools.getColor(R.color.text_lgray));
                            holder.setTextColor(R.id.tv_contact_name, Tools.getColor(R.color.text_lgray));
                            holder.setTextColor(R.id.tv_goods_info, Tools.getColor(R.color.text_lgray));
                        }
                        if ("0".equals(batchBean.fee2Id)) {
                            holder.setText(R.id.tv_fee_1, "免门票");
                        } else if ("1".equals(batchBean.fee2Id)) {
                            holder.setText(R.id.tv_fee_1, "未收门票");
                        } else if (batchBean.fee2Id.startsWith("33")) {
                            holder.setText(R.id.tv_fee_1, "未收门票");
                        } else if ("55".equals(batchBean.fee2Id)) {
                            holder.setText(R.id.tv_fee_1, "已收门票");
                        } else {
                            holder.setText(R.id.tv_fee_1, "未知门票");
                        }
                        if ("0".equals(batchBean.fee3Id)) {
                            holder.setText(R.id.tv_fee_2, "免进场费");
                        } else if ("1".equals(batchBean.fee3Id)) {
                            holder.setText(R.id.tv_fee_2, "未收进场费");
                        } else if (batchBean.fee3Id.startsWith("33")) {
                            holder.setText(R.id.tv_fee_2, "未收进场费");
                        } else if ("55".equals(batchBean.fee3Id)) {
                            holder.setText(R.id.tv_fee_2, "已收进场费");
                        } else {
                            holder.setText(R.id.tv_fee_2, "未知进场费");
                        }
                    }
                    chile.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(OutCarDetailActivity.this, BatchDetailActivity.class);
                            intent.putExtra("inBatchId", batchBean.inBatchId);
                            intent.putExtra("inCarId", inCarBean.inCarId);
                            startActivity(intent);
                        }
                    });
                    llContent.addView(chile);
                }
            }
        });
    }


    @Override
    public void loadInCarListSuccess(ArrayList<NetOutInCarBean> inCarList) {
        mCommonAdapter.getDatas().clear();
        mCommonAdapter.getDatas().addAll(inCarList);
        mCommonAdapter.notifyDataSetChanged();
    }

    @Override
    public void toOutCarSuccess() {
        mPresenter.loadInCarList(queryType, queryId, plateNum, containNo, hangNum, contactName);
    }

}
